package com.lnkj.spcj.base;

import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.lnkj.spcj.R;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

public class WebViewActivity2 extends BaseActivity2 {

    ImageView ivLeft;
    TextView tvTitle;
    WebView wv;
    LinearLayout ll_topbar;
    ProgressBar progressbar;
    String title;
    String content;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_web_view2;
    }

    @Override
    protected void initAll() {
        title = getIntent().getStringExtra("title");
        content = getIntent().getStringExtra("content");
        ivLeft = findViewById(R.id.iv_left);
        tvTitle = findViewById(R.id.tv_title);
        wv = findViewById(R.id.wv);
        progressbar = findViewById(R.id.pb);
        ll_topbar = findViewById(R.id.ll_topbar);
        tvTitle.setText(title + "");
        ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void processLogic() {
        wv.setWebViewClient(new MyWebViewClient());
        wv.setWebChromeClient(new MyWebChromeClient());
        WebSettings webSettings = wv.getSettings();
        webSettings.setAppCacheEnabled(true);
        webSettings.setJavaScriptEnabled(true);// 设置支持javascript脚本
//        webSettings.setBuiltInZoomControls(true);// 设置支持缩放
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);// 图片被缩放后显示不全的问题
        //自适应屏幕
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        wv.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);

        wv.loadData(content, "text/html; charset=UTF-8", null);//这种写法可以正确解码
//        wv.loadUrl(getIntent().getStringExtra("url"));
    }

    @Override
    protected void setListener() {

    }

    public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("intent") || url.startsWith("youku")) {
                return true;
            } else {
                return super.shouldOverrideUrlLoading(view, url);
            }
        }
    }

    public class MyWebChromeClient extends WebChromeClient {
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
//            tvTitle.setText(title);
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressbar.setVisibility(GONE);
            } else {
                if (progressbar.getVisibility() == GONE && TextUtils.isEmpty(getIntent().getStringExtra("urlParams")))
                    progressbar.setVisibility(VISIBLE);
                progressbar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }
    }

    @Override
    public void onBackPressed() {
        if (wv.canGoBack()) {
            wv.goBack();
        } else {
//            wv.destroy();
            super.onBackPressed();
        }
    }
}
